package com.test;

import io.netty.util.HashedWheelTimer;
import io.netty.util.Timeout;
import io.netty.util.TimerTask;

import java.util.concurrent.TimeUnit;

/**
 * https://blog.csdn.net/wangyang_software/article/details/54135704
 * Created by yanl_ on 2018/5/4.
 */
public class NettyTimerTask {

    //定义HashedWheelTimer对象
    //1000毫秒跳到下一个solt,一共有512个solt
    static HashedWheelTimer timer=new HashedWheelTimer(5000L, TimeUnit.MILLISECONDS, 512);

    public static void main(String[] args) {
        TimerTask task=new  TimerTask() {
            @Override
            public void run(Timeout timeout) throws Exception {
                System.out.println("---run service-----");
                //任务执行完成后再把自己添加到任务solt上
                addTask(this);
            }
        };
        addTask(task);
    }

    public static void addTask(TimerTask task){
        //根据时长把task任务放到响应的solt上
        timer.newTimeout(task, 500, TimeUnit.MILLISECONDS);
    }
}
